#!/bin/bash
#SBATCH --spread-job
# Copyright (c) 2020 Presto Labs Pte. Ltd.

if [ -z ${SLURM_JOB_ID} ]; then
  (>&2 echo "ERROR: run this script on slurm.")
  exit 1
fi

set -e 
export PYTHONUNBUFFERED=1
REMOTE_HOME={{remote_home}}
source ${REMOTE_HOME}/miniconda3/etc/profile.d/conda.sh
conda activate ${REMOTE_HOME}/{{coin_env}}

NUM_PARALLEL="$((${SLURM_NTASKS} + 30))"
parallel -j "${NUM_PARALLEL}" --delay 0.1 <<EOF
{% for strat_info in strat_info_list %}
srun -N1 -n1 -l --mem-per-cpu={{strat_info['mem-per-cpu']}} --exclusive --unbuffered \
  ./pyrunner coin/support/pta/app/pta_dumper_runner.py \
    {% if start_time is not none -%}
      --start_time={{start_time}} \
    {%- else -%}
      \
    {%- endif %}
    {% if end_time is not none -%}
      --end_time={{end_time}} \
    {%- else -%}
      \
    {%- endif %}
    --strategy_config_filename={{strat_info['name']}} \
    --max_workers 1 \
    {% if ignore_error == true -%}
      --ignore_error \
    {%- else -%}
      \
    {%- endif %}
    {% if offset_hour is not none -%}
      --offset_hour={{offset_hour}} \
    {%- else -%}
      \
    {%- endif %}
    {% if aggregate == true -%}
      --aggregate \
    {%- else -%}
      \
    {%- endif %}
    {% if interval_hour is not none -%}
      --interval_hour={{interval_hour}} \
    {%- else -%}
      \
    {%- endif %}
    {% if root_dir is not none -%}
      --root_dir={{root_dir}} \
    {%- else -%}
      \
    {%- endif %}
    {% if kafka_config_filename is not none -%}
      --kafka_config_filename={{kafka_config_filename}} \
    {%- else -%}
      \
    {%- endif %}
    {% if pnl_adj_file is not none -%}
      --pnl_adj_file={{pnl_adj_file}} \
    {%- else -%}
      \
    {%- endif %}
    {% if realtime_pnl_adj == true -%}
      --realtime_pnl_adj \
    {%- else -%}
      \
    {%- endif %}
    {% if pta_db_config is not none -%}
      --pta_db_config={{pta_db_config}} \
    {%- else -%}
      \
    {%- endif %}
    {% if latency_db_config is not none -%}
      --latency_db_config={{latency_db_config}} \
    {%- else -%}
      \
    {%- endif %}
    {% if output_file is not none -%}
      --output_file={{output_file}} \
    {%- else -%}
      \
    {%- endif %}
    {% if log_history_dir is not none -%}
      --log_history_dir={{log_history_dir}} \
    {%- else -%}
      \
    {%- endif %}
    {% if plot_dir is not none -%}
      --plot_dir={{plot_dir}} \
    {%- else -%}
      \
    {%- endif %}
    {% if plot_order == true -%}
      --plot_order \
    {%- else -%}
      \
    {%- endif %}
{% endfor %}
EOF
